package com.pinyougou.sellergoods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pinyougou.group.Specification;
import com.pinyougou.mapper.TbSpecificationMapper;
import com.pinyougou.mapper.TbSpecificationOptionMapper;
import com.pinyougou.pojo.*;
import com.pinyougou.pojo.TbSpecificationExample.Criteria;
import com.pinyougou.sellergoods.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;


/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
public class SpecificationServiceImpl implements SpecificationService {

	@Autowired
	private TbSpecificationMapper specificationMapper;
	@Autowired
	private TbSpecificationOptionMapper optionMapper;
	
	/**
	 * 查询全部
	 */
	@Override
	public List<TbSpecification> findAll() {
		return specificationMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<TbSpecification> page=   (Page<TbSpecification>) specificationMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(Specification specification) {
		//将规格添加到数据库中
		specificationMapper.insert(specification.getSpec());
		// 执行完上面的操作后，下面的规格id才会有值
		//前提是在TbSpecificationMapper.xml的insert方法中要有如下配置：
// 		<insert id="insert" parameterType="com.pinyougou.pojo.TbSpecification" >
//    	<selectKey resultType="java.lang.Long" order="AFTER" keyProperty="id">
//				SELECT LAST_INSERT_ID() AS id
//    	</selectKey>
		//用户输入的规格对象的id
		Long id = specification.getSpec().getId();
		//添加规格选项
		List<TbSpecificationOption> specificationOptionList = specification.getSpecificationOptionList();
		//循环遍历规格选项
		for (TbSpecificationOption option : specificationOptionList) {
			//将规格的id，赋值给规格选项中的规格id
			option.setSpecId(id);
			//插入规格选项
			optionMapper.insertSelective(option);
		}

	}
	/**
	 * 修改
	 */
	@Override
	public void update(Specification specification){
		//修改规格
		specificationMapper.updateByPrimaryKey(specification.getSpec());
		//更改规格选项值
		//思路：根据外键（规格id）先删除原来的，再添加新的
		//先删除原来的规格选项（从tb_specification_option表中删除）
		TbSpecificationOptionExample example=new TbSpecificationOptionExample();
		TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
		criteria.andSpecIdEqualTo(specification.getSpec().getId());
		optionMapper.deleteByExample(example);
		//添加规格选项
		//从组合对象中获取规格选项集合
		List<TbSpecificationOption> specificationOptionList = specification.getSpecificationOptionList();
		//循环遍历规格选项
		for (TbSpecificationOption option : specificationOptionList) {
			//将规格的id，赋值给规格选项中的规格id
			option.setId(specification.getSpec().getId());
			//插入规格选项
			optionMapper.insertSelective(option);
		}

	}	
	
	/**
	 * 根据ID获取组合实体
	 * @param id
	 * @return
	 */
	@Override
	public Specification findOne(Long id){
		//查询规格对象
		TbSpecification spec = specificationMapper.selectByPrimaryKey(id);
		//查询规格选项对象
		TbSpecificationOptionExample example=new TbSpecificationOptionExample();
		TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
		criteria.andSpecIdEqualTo(id);
		List<TbSpecificationOption> specificationOptions = optionMapper.selectByExample(example);
		//构造要返回的对象
		Specification specification = new Specification();
		specification.setSpec(spec);
		specification.setSpecificationOptionList(specificationOptions);
		//返回组合对象
		return specification;
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Long[] ids) {
		for (Long id : ids) {
			//删除规格
			specificationMapper.deleteByPrimaryKey(id);
			//根据规格id来删除规格选项,级联删除
			TbSpecificationOptionExample example=new TbSpecificationOptionExample();
			TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
			criteria.andSpecIdEqualTo(id);
			optionMapper.deleteByExample(example);
		}
	}
	
	
		@Override
	public PageResult findPage(TbSpecification specification, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		TbSpecificationExample example=new TbSpecificationExample();
		Criteria criteria = example.createCriteria();
		
		if(specification!=null){			
						if(specification.getSpecName()!=null && specification.getSpecName().length()>0){
				criteria.andSpecNameLike("%"+specification.getSpecName()+"%");
			}
	
		}
		
		Page<TbSpecification> page= (Page<TbSpecification>)specificationMapper.selectByExample(example);
		return new PageResult(page.getTotal(), page.getResult());
	}

	//查询所有的规格，格式为：[{id:111,text:"aaa"}...]
	@Override
	public List<Map> findSpecList() {
		return specificationMapper.findSpecList();
	}

}
